package com.situ.tos2024.api;

import com.github.pagehelper.PageInfo;
import com.situ.tos2024.model.Store;
import com.situ.tos2024.service.StoreService;
import com.situ.tos2024.util.JsonResult;
import com.situ.tos2024.util.PaginateInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 功能：
 *
 * @author 千堆雪
 * @version 1.0.0
 * @since 2024/4/10
 * <p>
 * created by 千堆雪 on 2024/4/10 16:26, last modified by 千堆雪 on 2024/4/10 16:26
 */
@RestController
@RequestMapping(value = "/api/v1/stores", produces = "application/json")
public class StoreApi {
    private StoreService storeService;

    @Autowired
    public void setStoreService(StoreService storeService) {
        this.storeService = storeService;
    }

    /**
     * 微服务接口，返回所有店铺信息
     */
    @GetMapping
    public JsonResult list(@RequestParam(defaultValue = "1", name = "page") Integer pageNo,
                           @RequestParam(defaultValue = "20", name = "limit") Integer pageSize,
                           Store store) {
        //创建一个分页对象
        PaginateInfo pi = PaginateInfo.of(pageNo, pageSize);

        //分页查询店铺
        List<Store> users = storeService.findAll(pi, store);

        //PageHelper的分页对象
        PageInfo<?> pif = pi.getPageInfo();

        return JsonResult.success(pif.getTotal(), users);
    }
}
